package com.tjbank.cssys.web.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tjbank.cssys.domain.CkOrderNotDialed;
import com.tjbank.cssys.domain.CkOrderRecoveryData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class intelligenceCallService {

    @Autowired
    private CkOrderRecoveryDataService ckOrderRecoveryDataService;
    @Autowired
    private CkOrderNotDialedService ckOrderNotDialedService;
    protected static final Logger logger = LoggerFactory.getLogger(intelligenceCallService.class);

    /**
     * 智能外呼反馈数据处理
     * @param filePath
     * @throws Exception
     */
    public void intelligenceCallService(String filePath) throws Exception {
        logger.info("智能外呼处理开始,参数为:"+filePath);
        logger.info("读取json文件开始");
        String intelligenceJsonStr = readJSONFile(filePath);
        logger.info("读取json文件结束,json文件为:"+intelligenceJsonStr);
        logger.info("处理未接及已接数据开始");
        JSONObject jsonObject = JSONObject.parseObject(intelligenceJsonStr);
        Object intelligenceObject = jsonObject.get("智能外呼");
        List<CkOrderRecoveryData> ckOrderRecoveryDataList = JSON.parseArray(JSON.toJSONString(intelligenceObject), CkOrderRecoveryData.class);
        //已接电话
        List<CkOrderRecoveryData> ckOrderRecoveryDatas = new ArrayList<>();
        //未接电话
        List<CkOrderNotDialed> ckOrderNotDialedList = new ArrayList<>();
        for (CkOrderRecoveryData ckOrderRecoveryData : ckOrderRecoveryDataList) {
            String feedbackCode = ckOrderRecoveryData.getFeedbackCode();
            if (StringUtils.isNotEmpty(feedbackCode)) {
                ckOrderRecoveryData.setNotificationMethod((byte) 2);
                ckOrderRecoveryDatas.add(ckOrderRecoveryData);
            } else {
                CkOrderNotDialed ckOrderNotDialed = new CkOrderNotDialed();
                BeanUtils.copyProperties(ckOrderRecoveryData,ckOrderNotDialed);
                ckOrderNotDialedList.add(ckOrderNotDialed);
            }
        }
        logger.info("处理未接及已接数据结束");
        logger.info("存储已接电话开始");
        ckOrderRecoveryDataService.insertForeach(ckOrderRecoveryDatas);
        logger.info("存储已接电话结束");
        logger.info("存储未接电话开始");
        ckOrderNotDialedService.insertForeach(ckOrderNotDialedList);
        logger.info("存储未接电话结束");
    }

    private String readJSONFile(String filePath) throws Exception {
        File file = new File(filePath);
        FileReader fileReader = new FileReader(file);
        Reader inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = inputStreamReader.read()) != -1) {
            sb.append((char) ch);
        }
        fileReader.close();
        inputStreamReader.close();
        String intelligenceJsonStr = sb.toString();
        return intelligenceJsonStr;
    }
}
